package com.codeoss.framework.view.service.impl;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bstek.ureport.export.ExportConfigure;
import com.bstek.ureport.export.ExportConfigureImpl;
import com.bstek.ureport.export.ExportManager;
import com.bstek.ureport.export.html.HtmlReport;
import com.codeoss.framework.context.YunContext;
import com.codeoss.framework.util.IOUtil;
import com.codeoss.framework.view.component.Component;
import com.codeoss.framework.view.component.report.UReport2;
import com.codeoss.framework.view.service.IUReport2ComponentService;
import com.codeoss.module.ir.model.IrReport;
import com.codeoss.module.ir.service.IIrReportService;


@Service("ureport2ComponentService")
public class UReport2ComponentService extends ComponentService implements IUReport2ComponentService,ApplicationContextAware{
    private ApplicationContext applicationContext;
    @Autowired
    private IIrReportService irReportService;
    @Value("${framework.base-path}")
	private String basePath;
    @Value("${ureport.fileStoreDir}")
   	private String fileStoreDir;
	@Override
	public void dataQuery(YunContext yunContext, Component component, Map<String, Object> paraMap) {
		// TODO Auto-generated method stub
		UReport2 ureport2=(UReport2)component;
		QueryWrapper<IrReport> query=new QueryWrapper<IrReport>();
		query.eq("code", ureport2.getReportCode());
		IrReport irReport=irReportService.getOne(query);
		
		ExportManager exportManager=(ExportManager)applicationContext.getBean(ExportManager.BEAN_ID);
		if(paraMap==null) {
			paraMap=new HashMap<String,Object>();
		}
		String filename=UUID.randomUUID().toString();
		File file=new File(fileStoreDir,filename);
		FileOutputStream fos;
		try {
			fos = new FileOutputStream(file);
			fos.write(irReport.getJsonContent().getBytes());
			fos.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}
		
		HtmlReport htmlReport=exportManager.exportHtml("file:"+filename, "", paraMap);
		yunContext.setAttribute(component.getId()+"_content", htmlReport.getContent());
		yunContext.setAttribute(component.getId()+"_style", htmlReport.getStyle());
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		// TODO Auto-generated method stub
		this.applicationContext=applicationContext;
	}

	@Override
	public String dataExport(YunContext yunContext, Component component, Map<String, Object> paraMap,String format) {
		// TODO Auto-generated method stub
		UReport2 ureport2=(UReport2)component;
		QueryWrapper<IrReport> query=new QueryWrapper<IrReport>();
		query.eq("code", ureport2.getReportCode());
		IrReport irReport=irReportService.getOne(query);
		
		ExportManager exportManager=(ExportManager)applicationContext.getBean(ExportManager.BEAN_ID);
		if(paraMap==null) {
			paraMap=new HashMap<String,Object>();
		}
		String filename=UUID.randomUUID().toString();
		File file=new File(fileStoreDir,filename);
		FileOutputStream fos;
		try {
			fos = new FileOutputStream(file);
			fos.write(irReport.getJsonContent().getBytes());
			fos.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}
		
		String uuid = UUID.randomUUID().toString();
		String reportFilename=null;
		if("pdf".equals(format)) {
			reportFilename = uuid + ".pdf";
		}else if("excel".equals(format)) {
			reportFilename = uuid + ".xlsx";
		}
		File reportFile=new File(basePath+"/tempfile"+File.separator+reportFilename);
		FileOutputStream reportFos=null;
		try {
			reportFos = new FileOutputStream(reportFile);
			ExportConfigure exportConfigure=new ExportConfigureImpl("file:"+filename,paraMap,reportFos);
			if("pdf".equals(format)) {
				exportManager.exportPdf(exportConfigure);
			}else if("excel".equals(format)) {
				exportManager.exportExcel(exportConfigure);
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}finally {
			IOUtil.closeOutputStream(reportFos);
		}
		return reportFilename;
	}

}
